package com.simu.towercrane.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVRelation;
import com.avos.avoscloud.FindCallback;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.simu.towercrane.R;
import com.simu.towercrane.fragments.ProductFragment;

public class NewsActivity extends BaseActivity{
	AVQuery<AVObject> query;
	private ArrayList<String> listImage = new ArrayList<String>();
	private List<Map<String, Object>> mList;
	int currentWhere;
	ImageView IVback;
	TextView TVtitle;
	TextView TVsourse;
	TextView TVtime;
	
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_newsdetial);
		Intent intent =getIntent();
		String objectID = intent.getStringExtra("ObjectID");
		ImageView IVback=(ImageView)findViewById(R.id.News_back_IV);
		initView(objectID);
IVback.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	
		
		 
	
		
	}

	public void initView(String objectID){
		query = new AVQuery<AVObject>("Article");
		query.whereEqualTo("objectId", objectID);
		query.findInBackground(new FindCallback<AVObject>() {

			@Override
			public void done(List<AVObject> list, AVException arg1) {
				// TODO Auto-generated method stub
				//mList = new ArrayList<Map<String, Object>>();
				//mList.clear();
				if (list != null) {

					for (int i = 0; i < list.size(); i++) {
						currentWhere = list.size();
						final HashMap<String, Object> map = new HashMap();
						map.clear();
						TVtitle=(TextView)findViewById(R.id.articile_title);
						TVtime=(TextView)findViewById(R.id.articile_time);
						TVsourse=(TextView)findViewById(R.id.articile_source);
						TVtitle.setText(list.get(i).getString("title"));
						TVsourse.setText(list.get(i).getString("source"));
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
						
						if(list.get(i).getDate("date")!=null)
						{
						String textday = sdf.format(list.get(i).getDate("date"));
						TVtime.setText(textday);
						}
						
						
						
						AVObject parent = list.get(i);
						//parent.getString("vendorName");

						AVRelation<AVObject> avRelation = parent
								.getRelation("imagetext");
						AVQuery<AVObject> avQuery = avRelation.getQuery();
						avQuery.findInBackground(new FindCallback<AVObject>() {

							@Override
							public void done(List<AVObject> arg0,
									AVException arg1) {
								if(arg0!=null)
								{
								
								ViewGroup group = (ViewGroup) 
										findViewById(R.id.viewGrou_newsdetials_ImageAndText);
								group.removeAllViews();
								for (int k = 0; k < arg0.size(); k++) {
									AVObject avobject = arg0.get(k);
									AVFile avFile = avobject.getAVFile("image");

									String url = avFile.getUrl();
									listImage.add(url);

									ImageView[] imageViews = new ImageView[listImage
											.size()];
									for (int i = 0; i < imageViews.length; i++) {
										final ImageView imageView = new ImageView(
												getApplicationContext());
										imageView
												.setLayoutParams(new LayoutParams(
														LayoutParams.MATCH_PARENT,
														LayoutParams.MATCH_PARENT));
										imageView.setPadding(0, 20, 0, 0);
										imageView.setScaleType(ScaleType.CENTER);
										imageViews[i] = imageView;
										ImageLoader.getInstance().displayImage(
												listImage.get(i) + "",
												imageViews[i]);
										// imageViews[i].setOnClickListener(new
										// OnClickListener() {
										//
										// @Override
										// public void onClick(View arg0) {
										// // TODO Auto-generated method stub
										// if(mImageListener!=null){
										// mImageListener.responeImageListner(imageView);
										// }
										// }
										// });
										String Text = arg0.get(k).getString(
												"text");
										TextView textview = new TextView(
												getApplicationContext());
										textview.setLayoutParams(new LayoutParams(
												LayoutParams.MATCH_PARENT,
												LayoutParams.MATCH_PARENT));
										textview.setPadding(20, 20, 20, 0);
										textview.setText(Text);
										textview.setTextColor(Color.BLACK);

										group.addView(imageView);
										group.addView(textview);
										listImage.clear();
										

									}

								}
								}

							}
						});

					}

				}

			}
		});
		
		
	}

}
